package com.jp.knowledge.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.adapter.WallertAdapter;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.Wallet;
import com.jp.knowledge.model.WalletList;
import com.jp.knowledge.util.ResultSubscriber;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiayen
 *
 * 钱包
 */
public class WalletActivity extends SlidingActivity implements ResultSubscriber.OnResultListener, View.OnClickListener, CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener {

    /**获取钱包基础信息标识**/
    private final int  GET_WALLERT_INFO=0x0000001;

    /***获取钱包使用记录标识*/
    private final int GET_WALLERT_LIST=0x00002;

    /**默认显示收到的金额*/
    private boolean   showGetMount=true;

    /***金额*/
    @ViewInject(R.id.amount)
    private TextView  amountView;

    /**收到的金额**/
    @ViewInject(R.id.get_amount)
    private TextView getMount;

    /***发出的金额*/
    @ViewInject(R.id.send_amount)
    private TextView  sendMount;


    /**刷新布局layout**/
    @ViewInject(R.id.refresh)
    private CanRefreshLayout  refreshLayout;


    /**内容布局*/
    @ViewInject(R.id.can_content_view)
    private RecyclerView  recyclerView;


    private WallertAdapter adapter;

    @Override
    protected int getResourceId() {
        return R.layout.activity_wallet;
    }

    private int get_page =1;
    private int send_page=1;

    private  List<WalletList>  getList;//收到列表记录
    private List<WalletList> sendList;//发出列表记录
    private boolean isloadMore=false;

    private   Wallet wallet;
    @Override
    protected void init() {

        leftIcon.setVisibility(View.VISIBLE);
        topName.setText("我的钱包");
        rightBtn.setText("提现");
        rightBtn.setVisibility(View.VISIBLE);
        rightIcon.setVisibility(View.GONE);


        leftIcon.setImageResource(R.mipmap.guanzhufanhui);
        rightBtn.setOnClickListener(this);
        leftIcon.setOnClickListener(this);
        getMount.setOnClickListener(this);
        sendMount.setOnClickListener(this);


        //初始化钱包数据
        initRefresh();

        //获取钱包基础信息数据
        getWalletInfo();
//
//        //获取钱包使用记录列表
//        getWalletList();
        autoGetList();
    }

    /**
     * 获取钱包基础信息
     */
    public void getWalletInfo() {
        JsonObject   param = new JsonObject();
        HttpHelper.getInstance(mContext).walletInfo(param,GET_WALLERT_INFO,this);
    }

    /**
     * 获取钱包使用记录列表
     */
    private void getWalletList(int type,int mode,int page) {
        JsonObject   param = new JsonObject();
        param.addProperty("type",type);
        param.addProperty("mode",mode);
        param.addProperty("page",page);
        HttpHelper.getInstance(mContext).walletList(param,GET_WALLERT_LIST,this);
    }

    /**
     * 初始化刷新布局
     */
    private void initRefresh() {
        refreshLayout.setRefreshEnabled(true);
        refreshLayout.setLoadMoreEnabled(true);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        adapter = new WallertAdapter(mContext,new ArrayList());
        recyclerView.setAdapter(adapter);
    }


    @Override
    public void onStart(int requestType) {

    }

    @Override
    public void onCompleted(int requestType) {
            refreshLayout.loadMoreComplete();
            refreshLayout.refreshComplete();
    }

    @Override
    public void onError(int requestType) {
        refreshLayout.loadMoreComplete();
        refreshLayout.refreshComplete();
        ToasUtil.toast(mContext,"获取钱包信息失败!");
    }

    @Override
    public void onNext(IModel data, int requestType) {
        int code =data.getErrcode();
        if(requestType==GET_WALLERT_INFO){//获取钱包基础信息
            if(code==0){
                 wallet = data.getEntity(Wallet.class);
                float  amount = wallet.getTotalAmount()/100f;
                amountView.setText(amount+"");
            }else {
                ToasUtil.toast(mContext,"获取钱包信息失败!");
            }
        }else if(requestType==GET_WALLERT_LIST){//获取钱包使用记录列表
            if(code==0){
                List<WalletList>  list = data.getList(WalletList.class);
                for (WalletList wallet:list ) {
                    wallet.setShowGetamount(showGetMount);
                }
                if(!isloadMore){
                    if(showGetMount){
                        getList = list;
                    }else{
                        sendList =list;
                    }
                }else{
                    if(showGetMount){
                            if(getList==null)getList = new ArrayList<>();
                        getList.addAll(list);
                    }else{
                        if(sendList==null)sendList = new ArrayList<>();
                        sendList.addAll(list);
                    }
                }
                if(showGetMount){
                    adapter.updataAdapter(getList);
                }else{
                    adapter.updataAdapter(sendList);
                }
            }else{
                ToasUtil.toast(mContext,"获取钱包记录列表信息失败!");
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.get_amount://收到的
                if(!showGetMount) {
                    showGetMount = true;
                    getMount.setBackgroundResource(R.drawable.awallet_blue_start);
                    sendMount.setBackgroundResource(R.drawable.awallet_blue_end_1);

                    getMount.setTextColor(getResources().getColor(R.color.white));
                    sendMount.setTextColor(getResources().getColor(R.color.theme_color));
                   autoGetList();
                }
                break;
            case R.id.send_amount://发出的
                if(showGetMount) {
                    showGetMount = false;
                    getMount.setBackgroundResource(R.drawable.awallet_blue_start_1);
                    sendMount.setBackgroundResource(R.drawable.awallet_blue_end);

                    getMount.setTextColor(getResources().getColor(R.color.theme_color));
                    sendMount.setTextColor(getResources().getColor(R.color.white));
                    autoGetList();
                }
                break;
            case R.id.right_bn://提现
                Intent intent = new Intent(mContext,WithdrawmoneyActivity.class);
                intent.putExtra("wallet",wallet);
                startActivity(intent);
                break;
            case  R.id.icon_left:
                this.finish();
                break;
            default:
                break;
        }
    }

    /**
     * 自动刷新列表
     */
    private void autoGetList(){
        refreshLayout.autoRefresh();
    }

    @Override
    public void onRefresh() {
        int type=showGetMount?1:2;
        int mode =0;
         int page = showGetMount?get_page+1:send_page+1;
        getWalletList(type,mode,page);
    }

    @Override
    public void onLoadMore() {
        int type=showGetMount?1:2;
        int mode =1;
        int page = showGetMount?get_page+1:send_page+1;
        getWalletList(type,mode,page);
        isloadMore=true;
    }
}
